
# Check if the packages that we need are installed
want = c("foreign","readstata13", "ggthemes","scales","stargazer",
         "gtools","tidyverse","interflex", "Cairo", "plm")

have = want %in% rownames(installed.packages())
# Install the packages that we miss
if ( any(!have) ) { install.packages( want[!have] ) }
# Load the packages
junk <- lapply(want, library, character.only = T)
# Remove the objects we created
rm(have, want, junk)


###### 
# BES DATA
###

rm(list = ls())

setwd("/Users/malet/Dropbox/Contagion paper/Analyses")

dat<-read.dta13("/Users/malet/Dropbox/Contagion paper/Replication files/bes_final.dta")

names(dat)


## FIGURE 7

# LOESS graph with three-point DV
dat$diff_support_const <- dat$support_const - dat$support_const_pre

sup<-filter(dat, timing<37)%>%
  group_by(timing, post_ref)%>%
  summarise(sup=mean(diff_support_const, na.rm = TRUE),
            size=dplyr::n())

ggplot(data = filter(dat, timing<37), aes(x=timing, y=diff_support_const)) +
  geom_point(data=subset(sup, post_ref==0), aes(y=sup,x=timing, color=post_ref, size=size)) +
  geom_point(data=subset(sup, post_ref==1), aes(y=sup,x=timing, color=post_ref, size=size)) +
  scale_size_area() +
  geom_smooth(data = subset(filter(dat, timing<37), post_ref==0)) + 
  geom_smooth(data = subset(filter(dat, timing<37), post_ref==1)) + 
  theme_bw()+  theme(legend.position = "none", text = element_text(size = 18)) + 
  ylab("Change in support for the Constitution") + xlab("Days from the French referendum")
ggsave("/Users/malet/Dropbox/Contagion paper/Graphs/bes_support_const.eps", device = cairo_ps,
       width = 15, height =  15, units = "cm")



###  FIGURE A4

# LOESS graph with dicothomized DV
dat$diff_support_constD <- dat$support_constD - dat$support_const_preD

sup<-filter(dat, timing<37)%>%
  group_by(timing, post_ref)%>%
  summarise(sup=mean(diff_support_constD, na.rm = TRUE),
            size=dplyr::n())

ggplot(data = filter(dat, timing<37), aes(x=timing, y=diff_support_constD)) +
  geom_point(data=subset(sup, post_ref==0), aes(y=sup,x=timing, color=post_ref, size=size)) +
  geom_point(data=subset(sup, post_ref==1), aes(y=sup,x=timing, color=post_ref, size=size)) +
  scale_size_area() +
  geom_smooth(data = subset(filter(dat, timing<37), post_ref==0)) + 
  geom_smooth(data = subset(filter(dat, timing<37), post_ref==1)) + 
  theme_bw()+  theme(legend.position = "none", text = element_text(size = 18)) + 
  ylab("Support for the Constitution") + xlab("Days from the French referendum")
ggsave("/Users/malet/Dropbox/Contagion paper/Graphs/bes_support_constD.eps", device = cairo_ps,
       width = 15, height =  15, units = "cm")


###  FIGURE A5
# LOESS graph with dicothomized DV= opposition to COnst
dat$against_const_preD <- ifelse(dat$support_const_pre==1,1,0)
dat$against_constD <- ifelse(dat$support_const==1,1,0)
dat$diff_against_constD <- dat$against_constD - dat$against_const_preD

opp<-filter(dat, timing<37)%>%
  group_by(timing, post_ref)%>%
  summarise(opp=mean(diff_against_constD, na.rm = TRUE),
            size=dplyr::n())

ggplot(data = filter(dat, timing<37), aes(x=timing, y=diff_against_constD)) +
  geom_point(data=subset(opp, post_ref==0), aes(y=opp,x=timing, color=post_ref, size=size)) +
  geom_point(data=subset(opp, post_ref==1), aes(y=opp,x=timing, color=post_ref, size=size)) +
  scale_size_area() +
  geom_smooth(data = subset(filter(dat, timing<37), post_ref==0)) + 
  geom_smooth(data = subset(filter(dat, timing<37), post_ref==1)) + 
  theme_bw()+  theme(legend.position = "none", text = element_text(size = 18)) + 
  ylab("Opposition to the Constitution") + xlab("Days from the French referendum")
ggsave("/Users/malet/Dropbox/Contagion paper/Graphs/bes_against_constD.eps", device = cairo_ps,
       width = 15, height =  15, units = "cm")





### FIGURE 8.A

# DV: support constitution
# interaction with previous eu attitudes: binning estimates

interflex(Y = "support_const", D = "post_ref", X = "support_eu_pre", Z="support_const_pre",
          na.rm = TRUE,
          data = dat, estimator = 'binning', 
          main = "", xlab = "Support for EU", ylab = "Treatment effect")
ggsave("/Users/malet/Dropbox/Contagion paper/Graphs/bes_inter_support_const_prevatt.eps", device = cairo_ps,
       width = 15, height =  15, units = "cm")


### FIGURE 8.B

# DV: support euro
# interaction with knowledge: binning estimates

interflex(Y = "euro_post", D = "post_ref", X = "knowledge", Z="euro_pre",
          na.rm = TRUE,
          data = dat, estimator = 'binning', #vartype = "robust", 
          main = "", xlab = "Knowledge", ylab = "Treatment effect")
ggsave("/Users/malet/Dropbox/Contagion paper/Graphs/bes_inter_euro_LDV_knowledge.eps", device = cairo_ps,
       width = 15, height =  15, units = "cm")




##### REPLICATE TABLES WITH PANEL DATA


## fixed effects models
dd1<-select_(dat, 
             "serialno","support_const","post_ref", "euro_post", 
             "window5", "window10", "window15", 
             "support_constD", "euro_postD", "against_constD")
dd1$wave<-2
dd1<-rename(dd1, eurozone = euro_post)
dd1<-rename(dd1, eurozoneD = euro_postD)

head(dd1)

dd2<-select_(dat, 
             "serialno","support_const_pre","post_ref", "euro_pre",
             "window5", "window10", "window15","support_const_preD","euro_preD","against_const_preD" )
dd2$wave<-1
dd2$post_ref<-0

dd2<-rename(dd2, support_const= support_const_pre)
dd2<-rename(dd2, eurozone= euro_pre)
dd2<-rename(dd2, support_constD= support_const_preD)
dd2<-rename(dd2, eurozoneD= euro_preD)
dd2<-rename(dd2, against_constD= against_const_preD)

head(dd2)

dd<-rbind(dd1,dd2)
head(dd)

dd$serialno<-factor(dd$serialno)
dd$wave<-factor(dd$wave)


###  TABLE 3

mod1 <- plm(support_const ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod2 <- lm(support_const ~ support_const_pre + post_ref, 
            data = dat)
mod3 <- plm(eurozone ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod4 <- lm(euro_post ~ euro_pre + post_ref, 
           data = dat)
stargazer(mod1,mod2,mod3,mod4, type="text")



###  TABLE 4
names(dat)
table(dat$eurosceptic_pre)
dat$eu_support_pre <- scales::rescale(dat$eurosceptic_pre, to=c())
mod1 <- lm(support_const ~ support_const_pre + post_ref*knowledge + post_ref*support_eu_pre, 
           data = dat)
mod2 <- lm(euro_post ~ euro_pre + post_ref*knowledge + post_ref*support_eu_pre, 
           data = dat)
stargazer(mod1,mod2,type="text")


### TABLE A10
mod1 <- plm(support_constD ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod2 <- plm(against_constD ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod3 <- plm(eurozoneD ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod4 <- lm(support_constD ~ support_const_preD + post_ref, 
            data = dat )
mod5 <- lm(against_constD ~ against_const_preD + post_ref, 
            data = dat )
mod6 <- lm(euro_postD ~ euro_preD + post_ref, 
            data = dat )


stargazer(mod1,mod2,mod3,mod4,mod5,mod6, type="text")





### TABLE A11

mod1 <- plm(support_const ~ post_ref, 
            data = subset(dd, window15==1),
            model = "within",
            index = c("serialno", "wave") )
mod2 <- plm(support_const ~ post_ref, 
            data = subset(dd, window10==1),
            model = "within",
            index = c("serialno", "wave") )
mod3 <- plm(support_const ~ post_ref, 
            data = subset(dd, window5==1),
            model = "within",
            index = c("serialno", "wave") )

ldv1 <- lm(support_const ~ support_const_pre + post_ref, data = subset(dat, window15==1))
ldv2 <- lm(support_const ~ support_const_pre + post_ref, data = subset(dat, window10==1))
ldv3 <- lm(support_const ~ support_const_pre + post_ref, data = subset(dat, window5==1))


stargazer(mod1,mod2,mod3,ldv1,ldv2,ldv3, type="text")



### TABLE A12

mod1 <- plm(eurozone ~ post_ref, 
            data = subset(dd, window15==1),
            model = "within",
            index = c("serialno", "wave") )
mod2 <- plm(eurozone ~ post_ref, 
            data = subset(dd, window10==1),
            model = "within",
            index = c("serialno", "wave") )
mod3 <- plm(eurozone ~ post_ref, 
            data = subset(dd, window5==1),
            model = "within",
            index = c("serialno", "wave") )
ldv4 <- lm(euro_post ~ euro_pre + post_ref, data = subset(dat, window15==1))
ldv5 <- lm(euro_post ~ euro_pre + post_ref, data = subset(dat, window10==1))
ldv6 <- lm(euro_post ~ euro_pre + post_ref, data = subset(dat, window5==1))
stargazer(mod4,mod5,mod6,ldv4,ldv5,ldv6, type="text")



### TABLE A13 - placebo outcomes
names(dat)
dd1<-select_(dat, 
             "serialno","post_ref", "labour", "tory", 
             "ideology", "iraq","attention",
             "window5", "window10", "window15" )
dd1$wave<-2
head(dd1)

dd2<-select_(dat, 
             "serialno","post_ref", "labour_pre", "tory_pre", 
             "ideology_pre", "iraq_pre","attention_pre",
             "window5", "window10", "window15" )
dd2$wave<-1
dd2$post_ref<-0

dd2<-rename(dd2, labour= labour_pre)
dd2<-rename(dd2, tory= tory_pre)
dd2<-rename(dd2, ideology= ideology_pre)
dd2<-rename(dd2, iraq= iraq_pre)
dd2<-rename(dd2, attention= attention_pre)

head(dd2)

dd<-rbind(dd1,dd2)
head(dd)

dd$serialno<-factor(dd$serialno)
dd$wave<-factor(dd$wave)



mod1 <- plm(labour ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod2 <- plm(tory ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod3 <- plm(ideology ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod4 <- plm(attention ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )
mod5 <- plm(iraq ~ post_ref, 
            data = dd,
            model = "within",
            index = c("serialno", "wave") )

stargazer(mod1,mod2,mod3,mod4,mod5, type = "text")



# TABLE A14
names(dat)

mod1 <- lm(support_const ~ support_const_pre + post_ref*party_pos, data = dat)
mod2 <- lm(euro_post ~ euro_pre + post_ref*party_pos, data = dat)
mod3 <- lm(eu_distance ~ eu_distance_pre + post_ref, data = dat)
stargazer(mod1,mod2,mod3, type = "text")


# further models not shown but mentioned in the paper
# interaction with party ID
mod1 <- lm(support_const ~ support_const_pre + post_ref*factor(pid), data = dat)
mod2 <- lm(euro_post ~ euro_pre + post_ref*factor(pid), data = dat)
stargazer(mod1,mod2, type = "text")
